package com.mdx.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdx.entity.EbookEntry;
import com.mdx.exception.SsmException;
import com.mdx.mapper.EntryMapper;
import com.mdx.service.EntryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class EntryServiceImpl implements EntryService {

    @Autowired
    private EntryMapper entryMapper;

    @Override
    public PageInfo<EbookEntry> getByCategoryId(Integer page,Integer size,Integer categoryid) {
        EbookEntry params=new EbookEntry();
        params.setCategoryid(categoryid);
        PageHelper.startPage(page,size);
        List<EbookEntry> entryList = entryMapper.select(params);
        PageInfo<EbookEntry> pageInfo=new PageInfo<>(entryList);
        return pageInfo;
    }

    @Override
    public EbookEntry getById(Integer id) {
        EbookEntry ebookEntry = entryMapper.selectByPrimaryKey(id);
        if (ebookEntry==null){
            throw new SsmException(231,"未查询出结果！");
        }
        return ebookEntry;
    }

    @Override
    public void delete(Integer id) {
        int count = entryMapper.deleteByPrimaryKey(id);
        if (count!=1){
            throw new SsmException(23,"删除失败");
        }
    }

    @Override
    public void update(EbookEntry ebookEntry) {
        int count = entryMapper.updateByPrimaryKeySelective(ebookEntry);
        if (count!=1){
            throw new SsmException(33,"修改失败");
        }
    }

    @Override
    public void add(EbookEntry ebookEntry) {
        int count = entryMapper.insertSelective(ebookEntry);
        if (count!=1){
            throw new SsmException(33433,"添加失败");
        }
    }
}
